package com.aplink.generic.pojo;

import java.util.ArrayList;

import android.text.TextUtils;

import com.aplink.generic.dictionary.GenericDictionary;

public class ContentData extends GenericDictionary<String, ObjPropInfo> {
	private static final long serialVersionUID = -511279188350326409L;
	public static ContentData getObjPropColOf(final String table,
	        final ContentData listObjPropCol) {
		final ContentData objPropCol = new ContentData();
		objPropCol.setMainTable(table);
		for (int i = 0; i < listObjPropCol.size(); i++) {
			final ObjPropInfo objPropInfo = listObjPropCol.valueAt(i);
			boolean canInsert = false;
			ObjPropInfo propInfo = null;
			if (objPropInfo.getTable().equalsIgnoreCase(table)) {
				canInsert = true;
				propInfo = new ObjPropInfo(objPropInfo);
			} else if (objPropInfo.isFKey()
			        && objPropInfo.getForeignTable().equalsIgnoreCase(table)) {
				canInsert = true;
				propInfo = new ObjPropInfo(objPropInfo);
				propInfo.setRelationship(null);
			}
			if (objPropCol.contains(objPropInfo)) {
				canInsert = false;
			}
			if (canInsert) {
				objPropCol.put(objPropInfo.getFullColumnName(), propInfo);
				if (objPropInfo.isCreatedTime()) {
					objPropCol.addCreatedTimeObject(propInfo);
				}
				if (objPropInfo.isSyncStatus()) {
					objPropCol.addSyncStatus(propInfo);
				}
				if (objPropInfo.isImageServerPath()) {
					objPropCol.setImageServerPath(propInfo);
				}
				if (objPropInfo.isImageLocalPath()) {
					objPropCol.setImageLocalPath(propInfo);
				}
				if (objPropInfo.isKey()) {
					objPropCol.addPrimaryKeys(propInfo);
				}
				if (objPropInfo.isFKey()
				        && (objPropInfo.getRelationship() != null)) {
					objPropCol.addForiegnKeys(propInfo);
				}
			}

		}
		return objPropCol;
	}
	private final ArrayList<ObjPropInfo> mCreatedTimeObject;
	private ArrayList<String> mDataRules;
	private final ArrayList<ObjPropInfo> mForiegnKeys;
	private final StringBuilder mGroupBy;
	private ObjPropInfo mImageLocalPath;
	private ObjPropInfo mImageServerPath;
	private final StringBuilder mJoins;
	private final ArrayList<String> mJoinTables;
	private ObjPropInfo mLatitudeCol;
	private ObjPropInfo mLongitudeCol;
	private String mParentTable;
	private final ArrayList<ObjPropInfo> mPrimaryKeys;
	private GenericDictionary<String, String> mRefreshTable;
	private StringBuilder mSortASCColumn;
	private StringBuilder mSortDSECColumn;
	private final ArrayList<ObjPropInfo> mSyncStatus;
	private String mWebserviceFunction;
	private final GenericDictionary<String, String> mWebServiceParameters;
	private ArrayList<String> queriedColumns = new ArrayList<String>();

	private ArrayList<ObjPropInfo> selectedColumn = new ArrayList<ObjPropInfo>();

	public ContentData() {
		mRefreshTable = new GenericDictionary<>();
		this.mPrimaryKeys = new ArrayList<ObjPropInfo>();
		this.mCreatedTimeObject = new ArrayList<ObjPropInfo>();
		this.mImageServerPath = new ObjPropInfo();
		this.mImageLocalPath = new ObjPropInfo();
		this.mSortASCColumn = new StringBuilder();
		this.mJoins = new StringBuilder();
		this.mSortDSECColumn = new StringBuilder();
		this.mSyncStatus = new ArrayList<ObjPropInfo>();
		this.mGroupBy = new StringBuilder();
		this.mJoinTables = new ArrayList<String>();
		this.mForiegnKeys = new ArrayList<ObjPropInfo>();
		setDataRules(new ArrayList<String>());
		mWebServiceParameters = new GenericDictionary<>();
	}

	public void addASCColumn(final String columnName) {
		if (this.mSortASCColumn.length() != 0) {
			this.mSortASCColumn.append(" , ");
		}
		this.mSortASCColumn.append(columnName);
	}

	public void addCreatedTimeObject(final ObjPropInfo createdTimeObject) {
		this.mCreatedTimeObject.add(createdTimeObject);
	}

	public void addDESCColumn(final String columnName) {
		if (this.mSortDSECColumn.length() != 0) {
			this.mSortDSECColumn.append(" , ");
		}
		this.mSortDSECColumn.append(columnName);
	}

	public void addForiegnKeys(final ObjPropInfo primaryKey) {
		this.mForiegnKeys.add(primaryKey);
	}

	public void addGroupBy(final String groupBy) {
		if (this.mGroupBy.length() != 0) {
			this.mGroupBy.append(" , ");
		}
		this.mGroupBy.append(groupBy);
	}

	public void addJoin(String leftTable, String joinType,
	        final String rightTable, final String onLeftCondition,
	        final String onRightCondition) {
		if ((leftTable == null) || (this.mJoins.length() != 0)) {
			leftTable = "";
		} else {
			if (!this.mJoinTables.contains(leftTable)) {
				this.mJoinTables.add(leftTable);
			}
		}
		if (!this.mJoinTables.contains(rightTable)) {
			this.mJoinTables.add(rightTable);
		}
		if (TextUtils.isEmpty(joinType)) {
			joinType = " INNER JOIN ";
		}
		if (joinType.equalsIgnoreCase("RIGHT JOIN")) {
			joinType = "LEFT JOIN";
			this.mJoins.append(rightTable).append(" " + joinType + " ")
			        .append(leftTable).append(" ON ").append(onRightCondition)
			        .append("=").append(onLeftCondition);
		} else {
			this.mJoins.append(leftTable).append(" " + joinType + " ")
			        .append(rightTable).append(" ON ").append(onLeftCondition)
			        .append("=").append(onRightCondition);
		}
	}

	public void addPrimaryKeys(final ObjPropInfo primaryKey) {
		this.mPrimaryKeys.add(primaryKey);
	}

	public void addSyncStatus(final ObjPropInfo syncStatus) {
		this.mSyncStatus.add(syncStatus);
	}

	public boolean contains(final ObjPropInfo objPropInfo) {
		String key = null;
		if (objPropInfo.isFKey() && (objPropInfo.getForeignTable() != null)) {
			key = objPropInfo.getForeignFullColumnName();
		} else {
			key = objPropInfo.getFullColumnName();
		}
		return containsKey(key);
	}

	public ObjPropInfo getCreatedTime(final String table) {
		for (final ObjPropInfo propInfo : this.mCreatedTimeObject) {
			if (propInfo.getTable().equalsIgnoreCase(table)) {
				return propInfo;
			}
		}
		return null;
	}

	public ArrayList<ObjPropInfo> getCreatedTimeObject() {
		return this.mCreatedTimeObject;
	}

	public ArrayList<String> getDataRules() {
		return this.mDataRules;
	}

	public ArrayList<ObjPropInfo> getForiegnKeys() {
		return this.mForiegnKeys;
	}

	public String getGroupBy() {
		return this.mGroupBy.toString();
	}

	public ObjPropInfo getImageLocalPath() {
		return this.mImageLocalPath;
	}

	public ObjPropInfo getImageServerPath() {
		return this.mImageServerPath;
	}

	public String getJoins() {
		return this.mJoins.toString();
	}

	public final ObjPropInfo getLatitudeCol() {
		return mLatitudeCol;
	}

	public final ObjPropInfo getLongitudeCol() {
		return mLongitudeCol;
	}

	public String getMainTable() {
		return this.mParentTable;
	}

	public String getOderBy() {
		String groupBy = null;
		if (this.mSortASCColumn.length() != 0) {
			groupBy = this.mSortASCColumn.toString() + " ASC";
		}
		if (this.mSortDSECColumn.length() != 0) {
			if (groupBy != null) {
				groupBy += "," + this.mSortDSECColumn.toString() + " DESC";
			} else {
				groupBy = this.mSortDSECColumn.toString() + " DESC";
			}
		}

		return groupBy;
	}

	public ObjPropInfo getPrimaryKey(final String table) {
		for (final ObjPropInfo propInfo : this.mPrimaryKeys) {
			if (propInfo.getTable().equalsIgnoreCase(table)) {
				return propInfo;
			}
		}
		return null;
	}

	public ArrayList<ObjPropInfo> getPrimaryKeys() {
		return this.mPrimaryKeys;
	}

	public ArrayList<String> getQueriedColumns() {
		return this.queriedColumns;
	}

	public GenericDictionary<String, String> getRefreshTable() {
		return mRefreshTable;
	}

	public ArrayList<ObjPropInfo> getSelectedColumn() {
		return this.selectedColumn;
	}

	public String getSortASCColumn() {
		return this.mSortASCColumn.toString();
	}

	public String getSortDSECColumn() {
		return this.mSortDSECColumn.toString();
	}

	public ArrayList<ObjPropInfo> getSyncStatus() {
		return this.mSyncStatus;
	}

	public ObjPropInfo getSyncStatus(final String table) {
		for (final ObjPropInfo propInfo : this.mSyncStatus) {
			if (propInfo.getTable().equalsIgnoreCase(table)) {
				return propInfo;
			}
		}
		return null;
	}

	public String getWebserviceFunction() {
		return this.mWebserviceFunction;
	}

	public String getWebserviceParameter(ObjPropInfo objPropInfo) {
		return mWebServiceParameters.get(objPropInfo.getFullColumnName());
	}

	public GenericDictionary<String, String> getWebServiceParameters() {
		return mWebServiceParameters;
	}

	public boolean hasImage() {
		return (this.mImageLocalPath != null)
		        || (this.mImageServerPath != null);
	}

	public boolean isContainTable(final String table) {
		return this.mJoinTables.contains(table);
	}

	public boolean isHasCreatedTime() {
		return this.mCreatedTimeObject.size() > 0;
	}

	public void setDataRules(final ArrayList<String> dataRules) {
		this.mDataRules = dataRules;
	}

	public void setImageLocalPath(final ObjPropInfo imageLocalPath) {
		this.mImageLocalPath = imageLocalPath;
	}

	public void setImageServerPath(final ObjPropInfo imageServerPath) {
		this.mImageServerPath = imageServerPath;
	}

	public final void setLatitudeCol(final ObjPropInfo latitudeCol) {
		mLatitudeCol = latitudeCol;
	}

	public final void setLongitudeCol(final ObjPropInfo longitudeCol) {
		mLongitudeCol = longitudeCol;
	}

	public void setMainTable(final String parentTable) {
		this.mParentTable = parentTable;
	}

	public void setQueriedColumns(final ArrayList<String> queriedColumns) {
		this.queriedColumns = queriedColumns;
	}

	public void setRefreshTable(
	        final GenericDictionary<String, String> refreshTable) {
		mRefreshTable = refreshTable;
	}

	public void setSelectedColumn(final ArrayList<ObjPropInfo> selectedColumn) {
		this.selectedColumn = selectedColumn;
	}

	public void setSortASCColumn(final StringBuilder sortASCColumn) {
		this.mSortASCColumn = sortASCColumn;
	}

	public void setSortDSECColumn(final StringBuilder sortDSECColumn) {
		this.mSortDSECColumn = sortDSECColumn;
	}

	public void setWebserviceFunction(final String webserviceFunction) {
		this.mWebserviceFunction = webserviceFunction;
	}
}
